/**
 * 
 */
package dt.inlee.common.interceptor;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import dt.inlee.common.constant.CommonConstant;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.domain.SysUser;
import dt.inlee.system.service.SysUserService;
import dt.inlee.system.vo.SysUserLoginBean;

/**
 *功能说明:过滤未登录用户直接通过走jsp页面访问资源
 * 
 * @作者 赵欢欢
 * 
 *     创建日期2010-6-9
 */

public class JspPathFilter implements Filter {
	@SuppressWarnings("unused")
	private FilterConfig filterConfig = null;

	@SuppressWarnings("unused")
    private static Logger log = Logger.getLogger(JspPathFilter.class);

	private HttpServletRequest req;

	private String MEDAccess = "";

	public String getMEDAccess() {
		return MEDAccess;
	}

	public void setMEDAccess(String mEDAccess) {
		MEDAccess = mEDAccess;
	}

	private SysUserService sysUserService;

	public void init(FilterConfig config) throws ServletException {
		this.filterConfig = config;
		setMEDAccess(config.getInitParameter("MEDAccess"));
	}

	@SuppressWarnings("unused")
    private SysUser load(String loginkey) {
		SysUser onlineUser = null;
		try {

			onlineUser = sysUserService.findByLoginKey(loginkey);
			if (onlineUser != null) {
				SysUserLoginBean sysUserLoginBean = new SysUserLoginBean();
				sysUserLoginBean.setId(onlineUser.getId());
				sysUserLoginBean.setUserName(onlineUser.getUserName());
				sysUserLoginBean.setMobileCode(onlineUser.getMobile());
//				sysUserLoginBean.setFlag(onlineUser.getFlag());
				sysUserLoginBean.setRealName(onlineUser.getRealName());

				req.getSession().setAttribute(CommonConstant.SESSION_USER,
						sysUserLoginBean);

				@SuppressWarnings("unchecked")
                Map<String, String> users = (Map<String, String>) req
						.getSession().getAttribute(
								CommonConstant.SERVLETCONTEXT_ONLINE_USER);

				if (users == null) {
					users = new HashMap<String, String>();
				}
				users.put(sysUserLoginBean.getUserName(), req.getSession()
						.getId());

				req.getSession().setAttribute(
						CommonConstant.SERVLETCONTEXT_ONLINE_USER, users);
			}
		} catch (FrameworkException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return onlineUser;
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException,
			IOException {
		req = (HttpServletRequest) request;
		String path = req.getServletPath();
		String igpath = path.substring(path.lastIndexOf("/") + 1);
		// 如果用户未登录，通过在IE地址栏走login.jsp或者register.jsp的页面可以直接访问资源，否则就进行拦截
		if (igpath.equalsIgnoreCase("login.jsp")
				|| igpath.equalsIgnoreCase("register.jsp") || igpath.equalsIgnoreCase("welcome.jsp")) {
			chain.doFilter(request, response);

		} else {
			// 如果用户已经登录，则用户可以在同一个IE浏览器通过url来访问资源，否则直接进入登录页面
			if (req.getSession().getAttribute(CommonConstant.SESSION_USER) != null) {
				chain.doFilter(request, response);
			} else {
				redirectLogin(req, response);
			}
		}
	}

	private void redirectLogin(ServletRequest request, ServletResponse response)
			throws IOException {
		HttpServletRequest req = (HttpServletRequest) request;
		String url = "<script language='javascript'>window.top.location.href='"
				+ req.getContextPath() + "/login.jsp'</script>";
		Writer writer = response.getWriter();
		writer.write(url);
		writer.flush();
		writer.close();
	}

	public void destroy() {
		this.filterConfig = null;
	}
}
